package com.zry.service.collect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zry.VO.CollectVo;
import com.zry.entity.Collect;
import com.zry.entity.CollectCategory;
import com.zry.mapper.CollectMapper;
import com.zry.service.collectCategory.ICollectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * stream 流实现查询的多种方式
 * stream 流的妙用
 * @author zry
 */

@Service("CollectServiceImplByStreamImpl")
@Slf4j
public class CollectServiceImplByStreamImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {

    @Autowired
    private ICollectCategoryService collectCategoryService;

    /**
     * 通过 key和 categoryList的id相同 得到categoryTitle
     * @return
     */
    @Override
    public List<CollectVo> findCollectByCategoryId() {

        List<CollectCategory> categoryList = collectCategoryService.list();

        LambdaQueryWrapper<Collect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Collect::getStatus,1);
        List<Collect> collectList = this.list(queryWrapper);

        Map<Integer, List<Collect>> listMap = collectList.stream().collect(Collectors.groupingBy(collect -> collect.getCategoryId()));

        List<CollectVo> collectVoList = new ArrayList<>();
        for (Map.Entry<Integer, List<Collect>> entry : listMap.entrySet()) {

            String categoryTitle = categoryList.stream().filter(collectCategory -> collectCategory.getId().equals(entry.getKey())).findFirst().get().getCategoryTitle();

            CollectVo collectVo = new CollectVo();
            collectVo.setCategoryId(entry.getKey());
            collectVo.setCategoryTitle(categoryTitle);
            collectVo.setCollectList(entry.getValue());
            collectVoList.add(collectVo);
        }

        return collectVoList;
    }

    /**
     * 使用 stream的 将categoryId分组 通过索引拿到 categoryTitle
     * @return
     */
    public List<CollectVo> findCollectByCategoryId(int method1) {

        LambdaQueryWrapper<Collect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Collect::getStatus,1);
        List<Collect> collectList = this.list(queryWrapper);

        Map<Integer, List<Collect>> listMap = collectList.stream().collect(Collectors.groupingBy(collect -> collect.getCategoryId()));

        List<CollectVo> collectVoList = new ArrayList<>();
        for (Map.Entry<Integer, List<Collect>> entry : listMap.entrySet()) {
            CollectVo collectVo = new CollectVo();
            collectVo.setCategoryId(entry.getKey());
            collectVo.setCategoryTitle(entry.getValue().get(0).getCategoryTitle());
            collectVo.setCollectList(entry.getValue());
            collectVoList.add(collectVo);
        }

        return collectVoList;
    }


}
